import Cookie from 'js-cookie'
import userinfo from '../../request/userInfo'
const PASS_PORT_BASE_URL = {
    dev: 'http://passport.dotransit.net',
    qa: 'http://t.passport.dotransit.net',
    prod: 'http://passport.dotransit.com',
    cn: 'http://passport.dotransit.cn'
};
/**
 * 校验用户信息
 */
function checkUserInfo(vue) {
    //首先校验cookie中是否存在token
    let token = Cookie.get("token");
    if (!token) {
        goToLogin(vue)
    }
    //校验token是否失效
    userinfo.checkToken().then(data => {
        // console.log(data)
    })
}
/**
 * 弹窗提示用户登录失效
 */
function goToLogin() {
    window.Vue.$Modal.info({
        title: "登录过期了",
        content: "当前用户登录已经过期,请前往重新登录！",
        onOk: function () {
            Cookie.remove('token', { domain: window.location.host });
            Cookie.remove('userinfo', { domain: window.location.host });
            window.location.href = getPassortUrl() + '?goto=' + window.location.href;
        }
    });
}
/**
 * 退出登录
 */
function loginOut() {
    userinfo.loginOut().then(data => {
        Cookie.remove('userInfo');
        Cookie.remove('token');
        window.location.href = getPassortUrl() + '?goto=' + window.location.href;
    })
}
/**
   * @name 获取web地址
   * @param {string} domain 域名
   * @param {string} env 环境
   */
function getPassortUrl() {
    return PASS_PORT_BASE_URL[process.env.BUILD_TO];
}
export default {
    checkUserInfo,
    goToLogin,
    loginOut
}